package ac.rfr.ui;

import javax.swing.JDialog;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
import java.util.Set;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class TemplatePreviewDialog extends JDialog {
	public TemplatePreviewDialog() {
		super();
		setModal(false);
		initComponents();
		setLocation();
	}

	private void initComponents()
	{
		setTitle("Template Preview");

		getContentPane().setLayout(new BorderLayout(0, 10));

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);

		okButton = new JButton("Ok");
		panel.add(okButton);

		JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, BorderLayout.CENTER);

		titleLabel = new JLabel("<HTML>Kindly preview the template you have configured in below table. " +
				"<br>Although table is editable changes made to values will not be reflected until tamplate is changed!</HTML>");
		scrollPane.setColumnHeaderView(titleLabel);
		table = new JTable();
		scrollPane.setViewportView(table);
		
		okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
	}

	public void setLocation()
	{
		Dimension	 scrSize = Toolkit.getDefaultToolkit().getScreenSize();
		setSize((int)((scrSize.getWidth())/4),(int)((scrSize.getHeight()*3)/4));
		setLocation((int)(scrSize.getWidth()/2 - getWidth()/2),(int)(scrSize.getHeight()/2 - getHeight()/2)); 
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel titleLabel;
	private JButton okButton;
	private JTable table;

	public void setColumns(Set<Object> columns)
	{
		for(int i = 0 ; i < table.getColumnCount() ; i++)
		{
			TableColumn tableColumn = table.getColumnModel().getColumn(i);
			table.removeColumn(tableColumn); 
		}

		int index = 0;
		for(Object columnHeader:columns)
		{
			TableColumn aColumn = new TableColumn(index++);
			aColumn.setHeaderValue(columnHeader);
			aColumn.sizeWidthToFit();
			table.addColumn(aColumn);
		}

		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				SwingUtilities.updateComponentTreeUI(table);
			}
		} );
	}
	
	public void showData(Properties properties)
	{
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		tableModel.setRowCount(0);
	
		Object[] paramNameArr = new Object[properties.keySet().size()];
		Object[] paramSizeArr = new Object[properties.keySet().size()];
		
		int i = 0;
		for(Object key: properties.keySet())
		{
			Object value = properties.get(key);
			paramNameArr[i] = key;
			paramSizeArr[i] = value;
			i++;
		}
		
		tableModel.addColumn("Parameter Name", paramNameArr);
		tableModel.addColumn("Parameter Length[Bytes]", paramSizeArr);

		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				
				SwingUtilities.updateComponentTreeUI(table);
			}
		} );

	}
	
	
}
